package com.kl.modular.sysman.system.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.kl.common.util.UUIDUtil;
import com.kl.modular.common.controller.BaseController;
import com.kl.modular.common.response.ResponseBase;
import com.kl.modular.system.dao.model.SystemDic;
import com.kl.modular.system.dao.model.SystemDicType;
import com.kl.modular.system.service.api.ISystemDicService;
import com.kl.modular.system.service.api.ISystemDicTypeService;
import com.kl.modular.system.service.bo.SystemDicBo;
import com.kl.modular.system.service.bo.SystemDicTypeBo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 数据字典控制器
 * @author xr_zjb
 * @date 2017-08-07 15:58
 * @(C) xinrui Copyright 
 */
@Controller
public class SystemDicController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(SystemDicController.class);
	
	@Autowired
	private ISystemDicService systemDicService;

	@Autowired
	private ISystemDicTypeService systemDicTypeService;
	
	/** 数据字典 view */
	private static String pathSystemDicRoot="/sysman/system/dic/";
	
	
	/**
	 * 数据字典列表页面
	 */
	@RequestMapping(value = "/sysman/systemDic/systemDicList", method = RequestMethod.GET)
	@RequiresPermissions("sysman:systemDic_man:view")
	public String queryList(HttpServletRequest request) {
		logger.info("数据字典列表页面");
		SystemDicTypeBo systemDicTypeBo = new SystemDicTypeBo();
		List<SystemDicType> systemDicTypeList = systemDicTypeService.selectListByBo(systemDicTypeBo);
		request.setAttribute("systemDicTypeList", systemDicTypeList);
		return pathSystemDicRoot + "systemDicList.ftl";
	}

	/**
	 * 数据字典列表数据
	 */
	@RequestMapping(value = "/sysman/systemDic/systemDicAjaxList", method = RequestMethod.POST)
	@RequiresPermissions("sysman:systemDic_man:view")
	public String queryAjaxList(SystemDicBo systemDicBo, Page<SystemDic> page, HttpServletRequest request) throws Exception {
		logger.info("systemDic列表数据>>data:" + JSON.toJSONString(systemDicBo));
		page = systemDicService.selectPageBySystemDicBo(page, systemDicBo);
		request.setAttribute("pager", page);
		return pathSystemDicRoot + "systemDicAjaxList.ftl";
	}
	
	
	/**
	 * 数据字典添加页面
	 * @return
	 */
	@RequestMapping(value="/sysman/systemDic/toSystemDicAdd" , method = RequestMethod.GET)
	@RequiresPermissions("sysman:systemDic_man:add")
	public String toAddSystemDic(HttpServletRequest request){
		logger.info("数据字典添加页面");
		SystemDicTypeBo dicTypeBo = new SystemDicTypeBo();
		List<SystemDicType> systemDicTypeList = systemDicTypeService.selectListByBo(dicTypeBo);
		request.setAttribute("systemDicTypeList" , systemDicTypeList);
		return pathSystemDicRoot + "addSystemDic.ftl";
	}
	
	/**
	 * 添加数据字典
	 */
	@RequestMapping(value="/sysman/systemDic/addSystemDic" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:systemDic_man:add")
	public String addSystemDic(@Valid SystemDic systemDic, BindingResult result, HttpServletResponse response) throws Exception{
		logger.info("添加数据字典>>data:" + JSON.toJSONString(systemDic));

		if (result.hasErrors()) {
			return ResponseBase.errorStr(result.getAllErrors().get(0).getDefaultMessage());
		}

		systemDic.setCreateTime(new Date());
		systemDic.setModifyTime(new Date());
		systemDic.setDicCode(UUIDUtil.getUniquekey());
		systemDicService.insert(systemDic);

		String jsonstr = ResponseBase.success();
		logger.info("添加数据字典>>返回>>data:" + jsonstr);
		return jsonstr;
	}

	
	/**
	 * 数据字典更新页面
	 * @return
	 */
	@RequestMapping(value = "/sysman/systemDic/toChangeView" , method = RequestMethod.GET)
	@RequiresPermissions("sysman:systemDic_man:update")
	public String toChangeSystemDic(Long id, HttpServletRequest request) throws Exception{
		logger.info("数据字典修改页面>>data:" + id);
		SystemDic systemDic = systemDicService.selectById(id);
		SystemDicTypeBo dicTypeBo = new SystemDicTypeBo();
		List<SystemDicType> systemDicTypeList = systemDicTypeService.selectListByBo(dicTypeBo);
		request.setAttribute("systemDicTypeList" , systemDicTypeList);
		request.setAttribute("systemDic", systemDic);
		return pathSystemDicRoot + "systemDicUpChange.ftl";
	}

	
	/**	
	 * 修改
	 */
	@RequestMapping(value = "/sysman/systemDic/upChange" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:systemDic_man:update")
	public String upChangeSystemDic(@Valid SystemDic systemDic, BindingResult result, HttpServletResponse response) throws Exception{
		logger.info("更新数据字典信息>>data:" + JSON.toJSONString(systemDic));

		if (result.hasErrors()) {
			return ResponseBase.errorStr(result.getAllErrors().get(0).getDefaultMessage());
		}
		if(systemDic.getId() == null){
			return ResponseBase.errorStr(ResponseBase.RESPOSNE_NOT_VALID_ERROR);
		}

		systemDic.setModifyTime(new Date());
		systemDicService.updateById(systemDic);

		String jsonstr = ResponseBase.success();
		logger.info("编辑数据字典信息>>返回>>data:" + jsonstr);
		return jsonstr;
	}

	/**
	 * 批量删除数据字典
	 */
	@RequestMapping(value = "/sysman/systemDic/delSystemDic" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:systemDic_man:delete")
	public String delSystemDic(String id,HttpServletResponse response) throws Exception{
		logger.info("批量删除数据字典>>data:" + JSON.toJSONString(id));

		// 数据验证
		if (StringUtils.isEmpty(id)) {
			return ResponseBase.errorStr(ResponseBase.RESPOSNE_NOT_VALID_ERROR);
		}

		String[] codestrs = id.split(",");

		systemDicService.logicalDeleteBatch(codestrs);

		String jsonstr = ResponseBase.success();
		logger.info("删除数据字典资源>>返回view>>data:" + jsonstr);
		return jsonstr;
	}

	/**
	 * 字典数据列表
	 * @param dicBo
	 * @return
	 */
	@RequestMapping(value = "/sysman/systemDic/innList" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:systemDic_man:view")
	public String innList(SystemDicBo dicBo){
		List<SystemDic> dicList = systemDicService.selectListByBo(dicBo);
		return JSON.toJSONString(dicList);
	}
}
